
//
//  SYBuyOptionsCell.m
//  PeiZiWangBei
//
//  Created by 巨彦新 on 2018/3/2.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDBuySection0Cell.h"

@interface YDBuySection0Cell()
/**
 股票名称
 */
@property (nonatomic, strong)UILabel *stockName;

/**
 提示图片
 */
@property (nonatomic, strong)UIImageView *questionImage;

/**
 搜索股票的背景
 */
@property (nonatomic, strong)UIView *searchView;
/**
 点击搜索股票
 */
@property (nonatomic, copy)UILabel *searchLbl;


/**
 详情
 */
@property (nonatomic, strong)UILabel *detailLbl;

@end

@implementation YDBuySection0Cell



-(void)setStockDic:(NSDictionary *)stockDic
{
    _stockDic = stockDic;
    
    NSString *stockName = [stockDic objectForKey:@"name"];
    NSString *stockCode = [stockDic objectForKey:@"instrumentId"];
    self.searchLbl.text = [NSString stringWithFormat:@"%@(%@)",stockName,stockCode];
    
    self.currentPrice.text =[NSString stringWithFormat:@"当前价：%.2f", [[stockDic objectForKey:@"lastPrice"] floatValue]];
    
    NSMutableAttributedString *attributestr = [[NSMutableAttributedString alloc] initWithString:self.currentPrice.text];
    UIColor *color;
    if ([stockDic[@"upDropPrice"] floatValue] == 0) {
        color = [UIColor colorWithHexString:dark];
    }else if ([stockDic[@"upDropPrice"] floatValue] > 0){
        color = [UIColor colorWithHexString:stockRed];
    }else{
        color = [UIColor colorWithHexString:leafyGreen];
    }
    [attributestr addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(4, self.currentPrice.text.length - 4)];
    self.currentPrice.attributedText = attributestr;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self stockName];
        [self questionImage];
        [self searchView];
        [self searchLbl];
        [self detailLbl];
        [self currentPrice];
    }
    return self;
}

- (void)tapClick:(UITapGestureRecognizer *)tap
{
    if (tap.view.tag == 100) {
        //点击了搜索view
        if (self.clickBack) {
            self.clickBack(@"搜索");
        }
    }else if (tap.view.tag == 101)
    {
        //点击了详情
        if (self.clickBack) {
            self.clickBack(@"详情");
        }
    }else if (tap.view.tag == 102)
    {
        //点击了问题icon
        if (self.clickBack) {
            self.clickBack(@"问号");
        }
    }
}
- (UILabel *)stockName
{
    if (!_stockName) {
        _stockName = [UILabel new];
        _stockName.text = @"标的股票";
        _stockName.textColor = [UIColor colorWithHexString:blueyGrey];
        _stockName.font = [UIFont systemFontOfSize:font14ScreenWRate *LKScreenW];
        [self.contentView addSubview:_stockName];
        [_stockName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(rate375(16));
            make.top.equalTo(self.contentView).offset(isPhoneX? rateX812(15) :rate750(15));
        }];
        
    }
    return _stockName;
}
-(UIImageView *)questionImage
{
    if (_questionImage == nil) {
        UIImageView *questtionImage = [[UIImageView alloc] init];
        [self.contentView addSubview:questtionImage];
        questtionImage.userInteractionEnabled = YES;
        [questtionImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.stockName.mas_right).offset(rate375(6));
            make.centerY.equalTo(self.stockName.mas_centerY);
        }];
        questtionImage.tag = 102;
        questtionImage.image = [UIImage imageNamed:@"zdqq-zhujie"];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        [questtionImage addGestureRecognizer:tap];
        
        _questionImage = questtionImage;
    }
    return _questionImage;
}


-(UIView *)searchView
{
    if (!_searchView) {
        _searchView = [UIView new];
        _searchView.backgroundColor = [UIColor colorWithHexString:mainGray];
        CGFloat w = rate375(180);
        CGFloat h = isPhoneX ? rateX812(40) :rate750(40);
        [self.contentView addSubview:_searchView];
        _searchView.tag = 100;
        [_searchView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(rate375(20));
            make.top.equalTo(self.stockName.mas_bottom).offset(isPhoneX? rateX812(10) :rate750(10));
            make.width.equalTo(@(w));
            make.height.equalTo(@(h));
        }];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        [_searchView addGestureRecognizer:tap];
        
    }
    return _searchView;
}
- (UILabel *)searchLbl
{
    if (!_searchLbl) {
        _searchLbl = [UILabel new];
        _searchLbl.text = @"平安银行（sz000001)";
        _searchLbl.textColor = [UIColor colorWithHexString:dark];
        _searchLbl.font = [UIFont systemFontOfSize:font14ScreenWRate *LKScreenW];
        [self.searchView addSubview:_searchLbl];
        [_searchLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.searchView);
        }];
        
    }
    return _searchLbl;
}

-(UILabel *)detailLbl
{
    if (!_detailLbl) {
        _detailLbl = [UILabel new];
//        _detailLbl.text = @"详情";
        _detailLbl.textColor = [UIColor blackColor];
        _detailLbl.font = [UIFont systemFontOfSize:font14ScreenWRate *LKScreenW];
         _detailLbl.userInteractionEnabled = YES;
        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"详情" attributes:@{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)}];
        
        _detailLbl.attributedText = attribtStr;
        
        [self.contentView addSubview:_detailLbl];
        _detailLbl.tag = 101;
        [_detailLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.searchView.mas_centerY);
            make.right.equalTo(self.contentView).offset(-rate375(20));
            
        }];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        [_detailLbl addGestureRecognizer:tap];
        
    }
    return _detailLbl;
}
-(UILabel *)currentPrice
{
    if (!_currentPrice) {
        _currentPrice = [UILabel new];
        _currentPrice.text = @"当前价：12.06";
        _currentPrice.textColor = [UIColor colorWithHexString:blueyGrey];
        _currentPrice.font = [UIFont systemFontOfSize:font14ScreenWRate *LKScreenW];
        [self.contentView addSubview:_currentPrice];
        [_currentPrice mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.searchView.mas_centerY);
            make.right.equalTo(self.detailLbl.mas_left).offset(-rate375(19));
       
        }];
        
    }
    return _currentPrice;
}
@end
